package async.complete;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

public class Demo02 {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(100);
        System.out.println(Thread.currentThread().getName());
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> { try {
            System.out.println("异步任务线程：" + Thread.currentThread().getName());
            int i = 1 / 0;
            TimeUnit.SECONDS.sleep(3);
            System.out.println("child run");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 123; },executor);
        System.out.println("main end");
        future.exceptionally(new Function<Throwable, Integer>() {
            @Override
            public Integer apply(Throwable throwable) {
                System.out.println("异常结果触发任务线程：" + Thread.currentThread().getName());
                System.out.println("异步任务执行失败：" + throwable.getMessage());
                return null;
            }
        });
    }
}
